package com.xbongbong.parent.field.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/07 14:25
 */
@Service("serialNoFieldAnalyseServiceImpl")
public class SerialNoFieldAnalyseServiceImpl implements FieldAnalyseService {

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.SERIALNO.getType());
    }

    @Override
    public Object analysisData(Long formId, Integer businessType, Integer saasMark, PaasFormDataEntityExt paasFormDataEntityExt, JSONObject data, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return paasFormDataEntityExt.getSerialNo();
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        if (Objects.equals(ConditionEnum.IN.getSymbol(), filterCondition.getSymbol())
                || Objects.equals(ConditionEnum.NOIN.getSymbol(), filterCondition.getSymbol())) {
            // 等于任意一个或者不等于任意一个
            String value = filterCondition.getValue().toString();
            List<String> valueList;
            if (value.contains(PaasConstant.CHINESE_SEMICOLON)) {
                String[] valueArray = value.split(PaasConstant.CHINESE_SEMICOLON);
                valueList = Arrays.asList(valueArray);
            } else if (filterCondition.getValue() instanceof List) {
                return filterCondition.getValue();
            } else {
                valueList = Arrays.asList(value);
            }
            return valueList;
        }
        return filterCondition.getValue();
    }
}
